package gogo

import (
	"go2/log"
	"net/http"
	"strconv"
)

var (
	mpHttpRouterHandler *HttpRouterHandler
)

func init() {
	if mpHttpRouterHandler == nil {
		mpHttpRouterHandler = NewHttpRouterHandler()
	}
}

// STATUS 注册指定HTTP状态触发的函数
// status int：触发状态
// handler HttpProcFunc: 触发函数
func STATUS(status int, handler func(*HTTPContext)) {
	mpHttpRouterHandler.RegHTTPStatusFunc(status, HttpProcFunc(handler))
}

// PUBLIC 注册PUBLIC请求，可以共享文件查看下载
// pattern string：触发路径
// handler HttpProcFunc: 触发函数
func PUBLIC(pattern, path string) {
	mpHttpRouterHandler.RegHTTPPublic(pattern, path)
}

// GET 注册GET请求
// pattern string：触发路径
// handler HttpProcFunc: 触发函数
func GET(pattern string, handler func(*HTTPContext)) {
	mpHttpRouterHandler.RegHTTPProcFunc("GET", pattern, handler)
}

// GET 注册固定URL前缀特征的GET钩子请求，主要用于自定义权限控制
// pattern string：触发路径
// handler preHttpProcFunc: 触发函数
func GET_PRE(pattern string, handler func(*HTTPContext) bool) {
	mpHttpRouterHandler.RegHTTPPreFunc("GET", pattern, handler)
}

// POST 注册POST请求
// pattern string：触发路径
// handler HttpProcFunc: 触发函数
func POST(pattern string, handler func(*HTTPContext)) {
	mpHttpRouterHandler.RegHTTPProcFunc("POST", pattern, handler)
}

// POST 注册固定URL前缀特征的POST钩子请求，主要用于自定义权限控制
// pattern string：触发路径
// handler preHttpProcFunc: 触发函数
func POST_PRE(pattern string, handler func(*HTTPContext) bool) {
	mpHttpRouterHandler.RegHTTPPreFunc("POST", pattern, handler)
}

// PUT 注册PUT请求
// pattern string：触发路径
// handler HttpProcFunc: 触发函数
func PUT(pattern string, handler func(*HTTPContext)) {
	mpHttpRouterHandler.RegHTTPProcFunc("PUT", pattern, handler)
}

// PUT 注册固定URL前缀特征的PUT钩子请求，主要用于自定义权限控制
// pattern string：触发路径
// handler preHttpProcFunc: 触发函数
func PUT_PRE(pattern string, handler func(*HTTPContext) bool) {
	mpHttpRouterHandler.RegHTTPPreFunc("PUT", pattern, handler)
}

// DELETE 注册DELETE请求
// pattern string：触发路径
// handler HttpProcFunc: 触发函数
func DELETE(pattern string, handler func(*HTTPContext)) {
	mpHttpRouterHandler.RegHTTPProcFunc("DELETE", pattern, handler)
}

// DELETE 注册固定URL前缀特征的DELETE钩子请求，主要用于自定义权限控制
// pattern string：触发路径
// handler preHttpProcFunc: 触发函数
func DELETE_PRE(pattern string, handler func(*HTTPContext) bool) {
	mpHttpRouterHandler.RegHTTPPreFunc("DELETE", pattern, handler)
}

// FORWARD_WITH_METHOD 注册指定HTTP方法的URL转发请求
// method string: 请求方式，POST、GET、PUT、DELETE等
// pattern string：触发路径
// target string: 目标URL
func POST_FORWARD(pattern, target string) {
	mpHttpRouterHandler.RegHTTPForward("POST", pattern, target)
}
func GET_FORWARD(pattern, target string) {
	mpHttpRouterHandler.RegHTTPForward("GET", pattern, target)
}
func PUT_FORWARD(pattern, target string) {
	mpHttpRouterHandler.RegHTTPForward("PUT", pattern, target)
}
func DELETE_FORWARD(pattern, target string) {
	mpHttpRouterHandler.RegHTTPForward("DELETE", pattern, target)
}

// FORWARD 注册URL转发请求
// pattern string：触发路径
// target string: 目标URL
func FORWARD(pattern, target string) {
	POST_FORWARD(pattern, target)
	GET_FORWARD(pattern, target)
	PUT_FORWARD(pattern, target)
	DELETE_FORWARD(pattern, target)
}

// StartHTTP 启动HTTP服务
func StartHTTP(port int) error {
	return StartHTTP2(&http.Server{
		Addr: ":" + strconv.Itoa(port),
	})
}

// StartHTTP 启动HTTP服务
// server: http.Server结构体，其中Handler参数不需要赋值，gogo会处理
func StartHTTP2(server *http.Server) error {
	log.Infof("GoGo Running on %s", server.Addr)
	server.Handler = mpHttpRouterHandler
	if err := server.ListenAndServe(); err != nil {
		return err
	}
	return nil
}

// StartHTTPS 启动HTTPS服务
func StartHTTPS(port int, certFile, keyFile string) error {
	return StartHTTPS2(&http.Server{
		Addr: ":" + strconv.Itoa(port),
	}, certFile, keyFile)
}

// StartHTTPS2 启动HTTPS服务
// server: http.Server结构体，其中Handler参数不需要赋值，gogo会处理
// certFile: 证书文件路径
// keyFile: 私钥文件路径
func StartHTTPS2(server *http.Server, certFile, keyFile string) error {
	log.Infof("GoGo Running on %s", server.Addr)
	server.Handler = mpHttpRouterHandler
	if err := server.ListenAndServeTLS(certFile, keyFile); err != nil {
		return err
	}
	return nil
}
